package de.justphil.tcg.tcgserver.rest.resources;



import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import org.slf4j.Logger;

import de.justphil.tcg.tcgserver.commons.exceptions.TCGException;
import de.justphil.tcg.tcgserver.rest.resources.base.AbstractResource;
import de.justphil.tcg.tcgserver.rest.resources.helpers.FileContentContainer;
import de.justphil.tcg.tcgserver.rest.resources.helpers.IndexHelper;

import static de.justphil.tcg.tcgserver.commons.logging.TCGLogging.*;

@Path("/")
public class IndexResource extends AbstractResource {
	private static final Logger log = restLogger(IndexResource.class);

	@GET
	@Produces(MediaType.TEXT_HTML)
	public Response index( @Context HttpHeaders headers ) {
		
		try {
			log.debug("Client requests IndexResource (index.htm)!");
			return ok( IndexHelper.getHtmlFile("index.htm") );
		}
		catch (TCGException e) {
			return err(e.getMessage(), (Status) e.getExtra(), headers.getAcceptableLanguages());
    	}
		
	}
	
	@GET
	@Path("/assets/{assetPath: [a-zA-Z0-9_\\-./]+[.][a-zA-Z0-9]{1,3}}")
	public Response assets( @PathParam("assetPath") String assetPath, @Context HttpHeaders headers ) {
		
		try {
			log.debug("Client requests IndexResource (assets)! assetPath = {}", assetPath);
			
			FileContentContainer fcc = IndexHelper.getAssetFile(assetPath);
			
			log.debug("mimeType = {}", fcc.getMimeType());
			
			return ok( fcc.getFileContent(), MediaType.valueOf( fcc.getMimeType() ) );
		}
		catch (TCGException e) {
			return err(e.getMessage(), (Status) e.getExtra(), headers.getAcceptableLanguages(), MediaType.APPLICATION_JSON_TYPE);
    	}
		
	}
	
}
